package com.jgc.locks;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2022/3/23.
 */
public class DeadLockDemo {
    static Object lockA = new Object();
    static Object lockB = new Object();

    public static void main(String[] args) {
        new Thread(()->{
           synchronized (lockA){
               System.out.println(Thread.currentThread().getName() + "\t" + "自己持有A锁，期待获取B锁");
               try {
                   TimeUnit.SECONDS.sleep(1);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               synchronized (lockB){
                   System.out.println(Thread.currentThread().getName()+"\t获得B锁成功");
               }
           }
        },"a").start();

        new Thread(()->{
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName() + "\t" + "自己持有B锁，期待获取A锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockA){
                    System.out.println(Thread.currentThread().getName()+"\t获得A锁成功");
                }
            }
        },"b").start();
    }
}
